package org.robot.scm.platform.monitor;

import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.robot.scm.pojos.config.BaseConfig;
import org.robot.scm.infra.RedisCacheHelper;
import org.robot.scm.platform.AbstractMonitor;
import org.robot.scm.pojos.BaseNotify;
import org.robot.scm.pojos.enums.PlatformEnum;
import org.robot.scm.pojos.platform.MessageBaseNotify;
import org.robot.scm.utils.JsonHelper;
import org.robot.scm.utils.SendNoticeHelper;

/**
 * 异系统解耦发送redis里面的消息
 */
@Slf4j
public class RedisNoticeMonitor extends AbstractMonitor {

    @Override
    public PlatformEnum getPlatform() {
        return PlatformEnum.REDIS;
    }

    @Override
    public BaseNotify doMonitor(BaseConfig config) {
        //redis-key 左入右出
        String keywords = config.getCurrentPlatform().getKeywords().get(0);
        String rpop = RedisCacheHelper.getInstance().rpop(keywords);
        try {
            MessageBaseNotify redisBean = JsonHelper.toObject(rpop, MessageBaseNotify.class);
            if (Objects.isNull(redisBean)) {
                return null;
            }
            SendNoticeHelper.sendNotice(config, redisBean, PlatformEnum.REDIS);
            return null;
        } catch (Exception e) {
            log.error("rpop info : {}", rpop, e);
        }
        return null;
    }
}
